package it.giuseppetrisciuoglio.lyrics.processor.artista;

import it.giuseppetrisciuoglio.lyrics.persistence.entity.Album;
import it.giuseppetrisciuoglio.lyrics.persistence.entity.Artista;
import it.giuseppetrisciuoglio.lyrics.persistence.entity.Canzone;
import it.giuseppetrisciuoglio.lyrics.persistence.entity.MetaDataInfoArtista;
import it.giuseppetrisciuoglio.lyrics.utils.DateUtilies;
import it.giuseppetrisciuoglio.lyrics.utils.StringUtilies;

import java.util.ArrayList;
import java.util.List;

import org.musicbrainz.android.api.data.Artist;
import org.musicbrainz.android.api.data.Release;
import org.musicbrainz.android.api.data.Track;
import org.musicbrainz.android.api.data.WebLink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component()
public class ArtistaAdapter {

	private final transient Logger log = LoggerFactory.getLogger(getClass());
	
	public Artista convertMusicbrainzEntity(Artist artist){
		log.debug("convertMusicbrainzEntity(): artist={}", artist);
		Artista artista = new Artista();
		artista.setMbid(artist.getMbid());
		artista.setNome(artist.getName());
		artista.setNazione(artist.getCountry());
		artista.setDataInizio(DateUtilies.getDateFromMusicbrainzDateString(artist.getStart()));
		artista.setDataFine(DateUtilies.getDateFromMusicbrainzDateString(artist.getEnd()));
		artista.setTipo(artist.getType());
		log.debug("convertMusicbrainzEntity(): artista={}", artista);
		return artista;
	}
	
	public List<MetaDataInfoArtista> convertMetaDataInfoArtistas(List<WebLink> links, Artista artista){
		log.debug("convertMetaDataInfoArtistas(): links={}, arista={}", links, artista);
		List<MetaDataInfoArtista> metaDataInfoArtistas = new ArrayList<MetaDataInfoArtista>();
		for (WebLink webLink : links) {
			MetaDataInfoArtista metaDataInfoArtista = new MetaDataInfoArtista();
			metaDataInfoArtista.setArtista(artista);
			metaDataInfoArtista.setTipo(StringUtilies.replaceParamType(webLink.getType()));
			metaDataInfoArtista.setValore(webLink.getUrl());
			metaDataInfoArtistas.add(metaDataInfoArtista);
		}
		log.debug("convertMetaDataInfoArtistas(): metaDataInfoArtistas={}", metaDataInfoArtistas);
		return metaDataInfoArtistas;
	}

	
	public Album convertReleaseToAlbum(Release release, Artista artista){
		log.debug("convertReleaseToAlbum(): release={}, arista={}", release, artista);
		Album album = new Album();
		album.setAnno(release.getDate());
		album.setTitolo(release.getTitle());
		album.setArtista(artista);
		album.setMbid(release.getMbid());
		List<Canzone> canzoni = new ArrayList<Canzone>();
		for (Track track : release.getTrackList()) {
			canzoni.add(convertTrackToCanzone(track, artista));
		}
		album.setCanzoni(canzoni);
		log.debug("convertReleaseToAlbum(): album={}", album);
		return album;
	}
	
	public Canzone convertTrackToCanzone(Track track, Artista artista){
		log.debug("convertTrackToCanzone(): track={}, arista={}", track, artista);
		Canzone canzone = new Canzone();
		canzone.setArtista(artista);
		canzone.setTitolo(track.getTitle());
		canzone.setTraccia(track.getPosition());
		canzone.setMbid(track.getRecordingMbid());
		log.debug("convertTrackToCanzone(): canzone={}", canzone);
		return canzone;
	}
	
}
